package com.chb.mains;

/**
 * @author CHB
 * @createTime 2022-6-27 16:26
 */
public class Find2ArraysNums {
    public static void main(String[] args) {
        int[][] matrix = {
                {1,4,7,11,15},
                {2,5,8,12,19},
                {3,6,9,16,22},
                {10,13,14,17,24},
                {18,21,23,26,30}};
        int target = 11;
        System.out.println(extracted(matrix, target));
    }

    private static boolean extracted(int[][] matrix, int target) {
        if(matrix.length <= 0 || matrix[0].length <= 0){
            return false;
        }
        int i = 0;
        int j = 0;
        // 纵向遍历
        while (true){
            if(matrix[i][j] == target){
                return true;
            }
            if(i < matrix.length - 1 && matrix[i][j] < target){
                i++;
            }else {
                break;
            }
        }
        while(true){
            int current = matrix[i][j];
            if(current == target){
                return true;
            }
            if(current > target){
                if(i > 0){
                    i--;
                }else {
                    return false;
                }
            }else {
                if(j < matrix[i].length - 1){
                    j++;
                }else {
                    return false;
                }
            }
        }
    }

}
